package org.itzheng.base;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.LayoutRes;

import org.itzheng.and.activity.ItActivity;
import org.itzheng.and.activity.ui.loading.ILoading;
import org.itzheng.and.activity.ui.loading.ItLoadingView;
import org.itzheng.and.activity.ui.toast.IToast;
import org.itzheng.ui.GrayView;


/**
 * Title:<br>
 * Description: <br>
 *
 * @email ItZheng@ZoHo.com
 * Created by itzheng on 2019-4-9.
 */
public class ItFragment extends androidx.fragment.app.Fragment implements ILoading, IToast {
    protected final String TAG = getClass().getSimpleName();
    protected View rootView;

    /**
     * 用于记录是否显示 onFirstVisible
     */
    private byte firstVisible = 0b00;
    /**
     * 只要执行过 onFirstVisible 这个方法就是false
     */
    protected boolean isFirstVisible = true;

    @Override
    public void onResume() {
        super.onResume();
        if (isFirstVisible) {
            //如果已经执行过onFirstVisible 就没必要判断了
            firstVisible = (byte) (firstVisible | 0b10);
            if (firstVisible == 0b11) {
                onFirstVisible();
            }
        }
    }

    /**
     * Tab 切换时调用
     *
     * @param hidden
     */
    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (isFirstVisible) {
            //如果已经执行过onFirstVisible 就没必要判断了
            if (!hidden) {
                firstVisible = (byte) (firstVisible | 0b01);
                if (firstVisible == 0b11) {
                    onFirstVisible();
                }
            }
        }

    }

    /**
     * 导入布局
     *
     * @param resId
     * @return
     */
    protected View inflate(@LayoutRes int resId) {
        return LayoutInflater.from(getActivity()).inflate(resId, null);
    }

    /**
     * setUserVisibleHint的使用场景:FragmentPagerAdapter+ViewPager
     *
     * @param isVisibleToUser
     */
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isFirstVisible) {
            //如果已经执行过onFirstVisible 就没必要判断了
            if (isVisibleToUser) {
                firstVisible = (byte) (firstVisible | 0b01);
                if (firstVisible == 0b11) {
                    onFirstVisible();
                }
            }
        }
    }

    /**
     * 第一次用户可见，即，setUserVisibleHint = true，onResume 两次都只执行一次时
     */
    protected void onFirstVisible() {
        isFirstVisible = false;

    }

    private Context mContext;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }

    @Override
    public Context getContext() {
        if (mContext != null) {
            return mContext;
        }
        return super.getContext();
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (null != rootView) {
            //只是将 rootView 移除，并没有置空，下次还可复用
            if (rootView.getParent() != null) {
                ((ViewGroup) rootView.getParent()).removeView(rootView);
            }

        }
    }

    /**
     * activity 执行 onBackPressed 的回调，需要主动在activity调用
     *
     * @return true 拦截返回事件，FALSE 不拦截，默认关闭当前窗口
     */
    public boolean onBackPressed() {
        //showloading的时候，我要如何判断是否可取消呢
        if (isLoading()) {
            if (isCancelLoading()) {
                dismissLoading();
            }
            return true;
        }
        return false;
    }

    /**
     * 是否可以取消，默认都是可以取消的
     *
     * @return
     */
    protected boolean isCancelLoading() {
        return true;
    }

    private ILoading loadingView;

    private ILoading getLoadingView() {
        if (loadingView == null) {
            loadingView = ItLoadingView.newInstance(getContext(), rootView);
        }
        return loadingView;
    }

    @Override
    public void showLoading() {
        getLoadingView().showLoading();
    }

    @Override
    public void dismissLoading() {
        getLoadingView().dismissLoading();
    }

    @Override
    public boolean isLoading() {
        return loadingView != null && getLoadingView().isLoading();
    }

    @Override
    public void showToast(int resId) {
        if (getActivity() instanceof ItActivity) {
            ItActivity baseActivity = (ItActivity) getActivity();
            baseActivity.showToast(resId);
        }
    }

    @Override
    public void showToast(String string) {
        if (getActivity() instanceof ItActivity) {
            ItActivity baseActivity = (ItActivity) getActivity();
            baseActivity.showToast(string);
        }
    }

    private GrayView masking;

    /**
     * 显示蒙版，灰色背景
     */
    public void showMasking() {
        if (masking == null) {
            masking = GrayView.newInstance(getContext(), rootView);
        }
        masking.show();
    }

    @Override
    public void onDetach() {
        super.onDetach();
        if (masking != null && masking.isShowing()) {
            masking.dismiss();
            masking = null;
        }
    }

    /**
     * 隐藏灰色背景
     */
    public void dismissMasking() {
        if (masking != null) {
            masking.dismiss();
        }
    }

    @Override
    public void finish() {

    }
}
